package com.myKng.content.repository;

import com.myKng.content.model.Knowledge;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface KnowledgeRepository extends JpaRepository<Knowledge, Long> {
    
    // 根据创建者查询知识列表
    Page<Knowledge> findByCreatedBy(String createdBy, Pageable pageable);
    
    // 根据分类ID查询知识列表
    Page<Knowledge> findByCategoryId(String categoryId, Pageable pageable);
    
    // 根据标签查询知识列表
    @Query("SELECT k FROM Knowledge k JOIN k.tags t WHERE t = :tag")
    Page<Knowledge> findByTag(String tag, Pageable pageable);
    
    // 根据标题或描述模糊查询
    Page<Knowledge> findByTitleContainingOrDescriptionContaining(String title, String description, Pageable pageable);
    
    // 增加浏览次数
    @Modifying
    @Query("UPDATE Knowledge k SET k.viewCount = k.viewCount + 1 WHERE k.id = :id")
    void incrementViewCount(Long id);
    
    // 查询公开的知识列表
    Page<Knowledge> findByIsPublic(Boolean isPublic, Pageable pageable);
    
    // 根据文件ID查询知识
    Optional<Knowledge> findByFileId(String fileId);
}